//make json post request, in difference with $getJSON this request is always encoded 
//in UTF8 Use it for every encoding sensible inputs, like key values for example  
function doPostJSON(url, data, callback){
	
	$.ajax({
	    type: "post",
	    url: url,
	    dataType: "json",
	    data: data,
	    success: callback,
	    error: function (XMLHttpRequest, textStatus, errorThrown) {
			alert('ERROR');
	    }
	});
	
}

//get the result of messages for the filter search and list
function searchProperties(page){
	$.getJSON(JsonUrls.SEARCH_RESULT,
							{"gameId":$('#'+'selectedGame').val(), 
							"langId": $('#'+'selectedLang').val(), 
							"empty":$('#'+'empty').attr('checked'),
							"outgrade":$('#'+'outgrade').attr('checked'),
							"page": page
							},
							function (data){
								drawSearchResult(data.results, 'properties', editionRowDrawer);
								createPagination(data.countPages,data.selectedPage,searchProperties);
							});
}

//get the list of pending translate messages for the filter lang
function searchPagedPendingTranslareProperties(page){
	$.getJSON(JsonUrls.PENDING_TRANSLATIONS,
							{"langId": $('#selectedLang').val(),
							 "page": page
							},
							function (data){
								drawSearchResult(data.results, 'properties', editionRowDrawer);
								createPagination(data.countPages,data.selectedPage,searchPagedPendingTranslareProperties);
							});
}

//search the common default property (those without a game  associated to the default language), filtering by the keypattern
function searchPagedCommonProperties(page){
	doPostJSON(JsonUrls.SEARCH_COMMON_KEYS,
			{"keyPattern": $('#'+'keyPattern').val(),
			 "gameId":$('#'+'gameId').val(),
			 "page": page
			},
			function (data){
				drawSearchResult(data.results, 'existentKeys', overwriteRowDrawer);
				createPagination(data.countPages,data.selectedPage,searchPagedCommonProperties);
			});
}

//draw a table with the data into the container with id = ContainerId
function drawSearchResult(data, containerId, drawerFunction){
	$('#'+containerId + ' > div').remove();
	var i;
	for(i = 0; i< data.length; i++){
		$('#'+containerId).append(drawerFunction(data[i]));
	}
}

//get json call for override a common key for an specific game 
function overwrite(propertyId, value){
	doPostJSON(JsonUrls.OVERWRITE_KEY,
			{"value": value,
			 "parentKeyId":$('#'+propertyId + ' > span >  input[name|="parentKeyId"]').val(),
			 "gameId":$('#gameId').val()
			},
			function (data){
				$('#'+propertyId + ' input[name|="button"]').attr('disabled','disabled');
			}
	);
	
}

function overwriteRowDrawer(property){
	var row = createResultRow(property.id);
	
	var cell = createResultCell('key');
	$(cell).append(property.key);
	$(row).append(cell);
	
	cell = createResultCell('default');
	$(cell).append($('<input type="hidden" name="parentKeyId" value="'+property.id+'"/>'));
	$(cell).append(property.value);
	$(row).append(cell);
	
	cell = createResultCell('editButton');
	$(cell).append(createButton(Keys.EDIT, function click(){
		loadEdition(property.id,overwrite);
		}
	));
	
	$(row).append(cell);
	
	return row;
}

function createButton(value, onClickFunction){
	return $('<input type="button" name="button"/>').attr('value',value).click(onClickFunction); 
}

function createResultRow(id){
	return $('<div class="search-result-row"></div>').attr('id',id);
}

function createResultCell(name){
	return $('<span class="search-result-cell"></span>').attr('name',name);
}

function editionRowDrawer(property){
	
	
	var row = createResultRow(property.id);
	var cell = createResultCell('key');
	$(cell).append(property.key);
	$(row).append(cell);
	
	cell = createResultCell('default');
	var defaultValue= property.keyParent != null ? defaultValue = property.keyParent.value : '';	  
	$(cell).append(defaultValue);
	$(row).append(cell);
	
	cell = createResultCell('value');
	$(cell).append(property.value);
	$(row).append(cell);
	
	cell = createResultCell('editButton');
	$(cell).append(createButton(Keys.EDIT, function click(){
		loadEdition(property.id,saveEdition);
		}
	));
	$(row).append(cell);
	
	
	return row;
}

function saveEdition(propertyId, value){
	doPostJSON(JsonUrls.SAVE_EDITION,
			{"propertyId":propertyId,
			 "value":value 
			},
			function (data){
				$('#'+propertyId + ' > img[name|="imageConfirm"]').remove()
				
				if(data){
					$('#'+propertyId + ' > *[name|="value"]').text(value);
					$('#'+propertyId).append('<img name="imageConfirm" src="'+Images.CHECK+'"/>');
				}else{
					$('#'+propertyId).append('<img name="imageConfirm" src="'+Images.CROSS+'"/>');
				}
			}
	);
}



